package com.example.rabbitmq.demo.config;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class RabbitMqConfig {

    /**
     * 图模库
     */
//    @Value("${gml.spring.rabbitmq.username:null}")
//    private String gmlName;
//    @Value("${gml.spring.rabbitmq.password:null}")
//    private String gmlPassword;
//    @Value("${gml.spring.rabbitmq.virtual-host:null}")
//    private String gmlHost;
    @Value("${gml.spring.rabbitmq.hostport:null}")
    private String gmlAddress;

    @Bean(name = "gmlConnectFactory")
    public ConnectionFactory gmlConnectFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(gmlAddress);
//        connectionFactory.setUsername(gmlName);
//        connectionFactory.setPassword(gmlPassword);
//        connectionFactory.setVirtualHost(gmlHost);
        return connectionFactory;
    }

    private SimpleRabbitListenerContainerFactory createRabbitListenerContainerFactory(
            ConnectionFactory connectionFactory, RabbitProperties rabbitProperties) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        RabbitProperties.Listener listener = rabbitProperties.getListener();
        factory.setAutoStartup(listener.isAutoStartup());
        if (listener.getAcknowledgeMode() != null) {
            factory.setAcknowledgeMode(listener.getAcknowledgeMode());
        }
        if (listener.getConcurrency() != null) {
            factory.setConcurrentConsumers(listener.getConcurrency());
        }
        if (listener.getMaxConcurrency() != null) {
            factory.setMaxConcurrentConsumers(listener.getMaxConcurrency());
        }
        if (listener.getPrefetch() != null) {
            factory.setPrefetchCount(listener.getPrefetch());
        }
        if (listener.getTransactionSize() != null) {
            factory.setTxSize(listener.getTransactionSize());
        }
        return factory;
    }

    @Bean(name = "gmlRabbitListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory gmlRabbitListenerContainerFactory(
            @Qualifier("gmlConnectFactory") ConnectionFactory gmlConnectFactory, RabbitProperties rabbitProperties) {
        return createRabbitListenerContainerFactory(gmlConnectFactory, rabbitProperties);
    }

    @Bean(name = "gmlAmqpAdmin")
    public AmqpAdmin gmlAmqpAdmin(@Qualifier("gmlConnectFactory") ConnectionFactory gmlConnectFactory) {
        RabbitAdmin admin = new RabbitAdmin(gmlConnectFactory);
        admin.setAutoStartup(false);
        return admin;
    }
}
